package com.hyt.it.ogt.data.service.pj.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.hyt.core.base.BaseServiceImpl;
import com.hyt.core.util.UUIDUtils;
import com.hyt.it.ogt.data.mapper.pj.PjDataSubjectScoreMapper;
import com.hyt.it.ogt.data.model.entity.pj.Subject;
import com.hyt.it.ogt.data.model.vo.pj.ObjectiveScoreVO;
import com.hyt.it.ogt.data.model.vo.pj.SubjectScore;
import com.hyt.it.ogt.data.service.pj.IPjDataSubjectScoreService;

import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 总分表 服务实现类
 * </p>
 *
 * @author liuqi
 * @date 2022-06-07
 */
@Service
public class PjDataSubjectScoreServiceImpl extends BaseServiceImpl<PjDataSubjectScoreMapper, SubjectScore> implements IPjDataSubjectScoreService {

    @Override
    public void saveObjectiveScore(String projectId, List<ObjectiveScoreVO> objectiveScoreVOs, List<Subject> subjects) {
    	List<String> subjectiveSubjectIds = new ArrayList<String>();
    	if(CollectionUtils.isNotEmpty(subjects)) {
    		for (Subject subject : subjects) {
    			subjectiveSubjectIds.add(subject.getSubjectId());
			}
    	}
        if(CollectionUtils.isNotEmpty(objectiveScoreVOs)){
            List<SubjectScore> subjectScoreList = new ArrayList<>();
            Map<String, ObjectiveScoreVO> dist = new HashMap<>();
            for(ObjectiveScoreVO objectiveScoreVO: objectiveScoreVOs){
            	if(subjectiveSubjectIds.contains(objectiveScoreVO.getSubjectId())) {
            	    String key = objectiveScoreVO.getProjectId() + objectiveScoreVO.getSubjectId() + objectiveScoreVO.getAdmissionNum();
            	    if(dist.containsKey(key)) {
            	        continue;
            	    }
            		SubjectScore subjectScore = new SubjectScore();
            		subjectScore.setId(UUIDUtils.newSortUUID());
                    subjectScore.setProjectId(objectiveScoreVO.getProjectId());
                    subjectScore.setSubjectId(objectiveScoreVO.getSubjectId());
                    subjectScore.setAdmissionNum(objectiveScoreVO.getAdmissionNum());
                    subjectScore.setEncodeId(objectiveScoreVO.getAdmissionNum());
                    subjectScore.setObjectiveScore(objectiveScoreVO.getKgScore());
                    subjectScore.setTotalScore(objectiveScoreVO.getKgScore());
                    subjectScoreList.add(subjectScore);
                    dist.put(key, objectiveScoreVO);
            	}
            }
            if(CollectionUtils.isNotEmpty(subjectScoreList)){
            	saveBatch(subjectScoreList);
            }
        }
    }

}
